package myoa

import myoa.domain.ExpenseGroupStatus
import myoa.excel.ExcelSheetName
import myoa.tool.StringTool

class ExpenseDetailController {

    ExcelImpService excelImpService
    FileStoreService fileStoreService

    def beforeInterceptor = [action: this.&readOnly, except: ['show']]

    //private will not display in Role page
    private readOnly() {
        if (actionName in ['create', 'save', 'upload', 'uploadDo']) {
            def group = ExpenseGroup.read(params.id)
            if (group.status != ExpenseGroupStatus.New) {
                flash.message = message(code: 'expense.read.only')
                redirect(controller: 'expenseGroup', action: 'show', id: group.id)
                return false //false=终止，不在继续进入后续action
            }
        } else if (actionName in ['edit', 'update', 'delete']) {
            def group = ExpenseDetail.read(params.id).expenseGroup
            if (group.status != ExpenseGroupStatus.New) {
                flash.message = message(code: 'expense.read.only')
                redirect(action: 'show', id: params.id)
                return false //false=终止，不在继续进入后续action
            }
        }
    }

//----------level 2.1 by following access control----------
    //id for group id
    def create(Long id) {
        [expenseGroup: ExpenseGroup.read(id)]
    }

    //id for group id
    def save(Long id) {
        //自动数据绑定的时候，123abc会被存为123、1.23会被存为1，不知道这算冗余还是算bug，暂时用这个办法来触发验证
        if (!params.payAmount.isInteger()) {
            params.payAmount = 'not integer'
        }
        ExpenseDetail ed = new ExpenseDetail(params)
        if (ed.save()) {
            flash.message = "expense detail #${ed.id} added"
            redirect(controller: 'expenseGroup', action: 'show', id: id)
        } else {
            flash.message = message(code: 'default.save.failed')
            redirect(action: 'create', id: id)
        }
    }

    //id for group id
    def upload(Long id) {
        [expenseGroup: ExpenseGroup.read(id)]
    }

    //id for group id
    def uploadDo(Long id) {
        FileStore fs = fileStoreService.upload()
        List<Map> exDetailList = excelImpService.imp(fs.id, ExcelSheetName.expenseDetail)
        int successCount = 0
        exDetailList.each {
            it.payDate = StringTool.parseExcelDate(it.payDate)
            ExpenseDetail exDetail = new ExpenseDetail(it)
            exDetail.expenseGroup = ExpenseGroup.get(id)
            if (exDetail.save()) {
                successCount++
            }
        }
        flash.message = "${successCount} of ${exDetailList.size()} expenses insert success"
        redirect(controller: 'expenseGroup', action: 'show', id: id)
    }

//----------level 2.2 by following access control----------
    def show(Long id) {
        [expenseDetail: ExpenseDetail.read(id)]
    }

    def edit(Long id) {
        [expenseDetail: ExpenseDetail.read(id)]
    }

    def update(Long id) {
        ExpenseDetail e = ExpenseDetail.get(id)
        e.properties = params

        if (e.save()) {
            flash.message = message(code: 'default.save.success')
            redirect(action: 'show', id: id)
        } else {
            flash.message = message(code: 'default.save.failed')
            render(view: "edit", model: [expenseDetail: e]) //todo 研究：render会自动传递params的值
        }
    }

    def delete(Long id) {
        ExpenseDetail e = ExpenseDetail.get(id)
        e.delete()
        flash.message = message(code: 'default.delete.success', args: [e.id])
        redirect(controller: 'expenseGroup', action: 'show', id: e.expenseGroup.id)
    }
}
